package com.example.a22310.myapplication.ViewModel.User;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Activity.LoginActivity;
import com.example.a22310.myapplication.Activity.RegisterActivity;
import com.example.a22310.myapplication.Beans.user;
import com.example.a22310.myapplication.Listener.UserListener;
import com.example.a22310.myapplication.Model.UserModel;

public class CheckPhoneNumberViewModel implements UserListener {
    private ActivityCheckPhoneNumberBinding binding;
    private AppCompatActivity activity;
    private user user;
    private int sign;
    private int flag = 0;

    public CheckPhoneNumberViewModel(AppCompatActivity activity, ActivityCheckPhoneNumberBinding binding){
        this.activity = activity;
        this.binding = binding;
        Initialize();
        binding.setModel(this);

    }


    public void onback(View view) {  //直接返回登录页面
        activity.onBackPressed();
    }

    private void Initialize(){
        Intent intent = activity.getIntent();
        Bundle bundle = intent.getExtras();
        sign = bundle.getInt("flag");
        if(sign == 0){
            binding.password.setVisibility(View.GONE);
            binding.comfirmPassword.setVisibility(View.GONE);
            binding.title.setText("注册");
        }else{
            binding.btnNextt.setText("确定");
            binding.title.setText("找回密码");
        }
    }

    public void textView_register(View view) {  //下一步按钮监听
        if(sign == 0){
            flag = 0;
            checkCode();
        }else{
            findPassword();
        }

    }
    /*
     *检验验证码是否正确
     * */
    private void checkCode(){
        user=new user();
        user.setUser_user(binding.phoneNumber.getText().toString());
        user.setVerify(binding.verificationCode.getText().toString());
        UserModel userModel=new UserModel();
        userModel.checkVerify(user,CheckPhoneNumberViewModel.this);
    }



    /*如果跳转过来是注册页面，验证手机号是否注册过，如果注册过，
     **则提示手机号已注册，否则发送验证码
     ** 如果跳转过来是忘记密码页面，验证手机号是否注册过，如果注册过，
     * *则进行找回密码操作，否则，提示该手机好还为注册过。
     * */
    public void check_phoneNumber(View view) {
        flag = 1;
        user=new user();
        user.setUser_user(binding.phoneNumber.getText().toString());
        UserModel userModel=new UserModel();
        userModel.isPhoneExsit(sign,user,CheckPhoneNumberViewModel.this);

    }


    private void findPassword(){   //重置密码

        user=new user();
        user.setUser_user(binding.phoneNumber.getText().toString());
        user.setVerify(binding.verificationCode.getText().toString());
        user.setUser_password(binding.password.getText().toString());
        if(!binding.password.getText().toString().equals(binding.comfirmPassword.getText().toString())) {
            Toast.makeText(activity, "两次密码不一直，请重新输入", Toast.LENGTH_LONG).show();
        }else{
            flag = 2;
            UserModel userModel=new UserModel();
            userModel.resetPwd(user,CheckPhoneNumberViewModel.this);
        }

    }


    @Override
    public void onSuccess(user user) {
        Initialize();
        if (flag == 0){
            if(sign == 0) {
                if (user.getStatus().equals("验证码已经失效")) {
                    Toast.makeText(activity, "该验证码已经失效，请重新验证", Toast.LENGTH_LONG).show();
                } else if (user.getStatus().equals("验证码成功")) {
                    Intent intent = new Intent(activity, RegisterActivity.class);
                    intent.putExtra("phone", binding.phoneNumber.getText().toString());
                    activity.startActivity(intent);
                } else if (user.getStatus().equals("验证码不正确")) {
                    Toast.makeText(activity, "验证码不正确", Toast.LENGTH_LONG).show();
                }
            }
        }
        if (1 == flag){
            if (user.getStatus().equals("已发送")) {
                //检验验证码操作
            }
            if (user.getStatus().equals("该手机号已注册过")) {

                Toast.makeText(activity, "该手机号已注册过", Toast.LENGTH_LONG).show();
            }
        }else{
            if (user.getStatus().equals("已发送")) {
                //检验验证码操作
            }
            if (user.getStatus().equals("该手机号没有被注册过")) {

                Toast.makeText(activity, "该手机号没有被注册过", Toast.LENGTH_LONG).show();
            }
        }
        if(flag==2) {
            if (user.getStatus().equals("验证码已经失效")) {
                Toast.makeText(activity, "该验证码已经失效，请重新验证", Toast.LENGTH_LONG).show();
            } else if (user.getStatus().equals("密码重置成功")) {
                Intent intent = new Intent(activity, LoginActivity.class);
                intent.putExtra("phone", binding.phoneNumber.getText().toString());
                intent.putExtra("phone", binding.password.getText().toString());
                activity.startActivity(intent);
            } else if (user.getStatus().equals("验证码不正确")) {
                Toast.makeText(activity, "验证码不正确", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(activity, "密码重置失败", Toast.LENGTH_LONG).show();
            }
        }
    }



    @Override
    public void onSuccess(UserModel user) {

    }

    @Override
    public void onFailure() {

    }
}